<template >
  <div>
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item
        label="用户名:"
        :label-width="formLabelWidth"
        prop="userName"
      >
        <el-input v-model="ruleForm.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="用户头像:" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="https://localhost:44366/api/UserRole/postFile"
          style="float: left"
          name="formFile"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="this.ruleForm.userPIc"
            :src="'https://localhost:44366/' + ruleForm.userPIc"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="手机号:" :label-width="formLabelWidth">
        <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码:" :label-width="formLabelWidth">
        <el-input
          v-model="ruleForm.password"
          autocomplete="off"
          show-password
        ></el-input>
      </el-form-item>
      <el-form-item label="角色:" :label-width="formLabelWidth">
        <el-select
          v-model="ruleForm.roleId"
          multiple
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in RoleList"
            :key="item.roleId"
            :label="item.roleName"
            :value="item.roleId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import axios from "@/unit/request";
export default {
  data() {
    return {
      ruleForm: {
        userName: "",
        userPIc: "",
        phone: "",
        password: "",
        roleId: [],
      },
      rules: {
        userName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
      RoleList: [],
      formLabelWidth: "120px",
    };
  },

  methods: {
    //添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.roleId = this.ruleForm.roleId.join(",");
          axios.post("/UserRole/Insert", this.ruleForm).then((res) => {
            if (res.data.code == 1) {
              this.$message.success(res.data.message);

              this.$emit("OnSave");
            } else {
              this.$message.error(res.data.message);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //绑定角色下拉
    GetRole() {
      axios.get("/Role/Query").then((res) => {
        this.RoleList = res.data.data;
      });
    },
    //图片上传
    handleAvatarSuccess(res) {
      this.ruleForm.userPIc = res;
    },
  },

  created() {
    this.GetRole();
  },
};
</script>
<style >
</style>